/* $Header: sampler.h,v 2.13 05/07/27 11:07:35 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME:  sampler.h                                       */
/* ==================                                                  */
/*                                                                     */
/* Author:  Tom Przybylski                                             */
/*                                                                     */
/* COMMENT:                                                            */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef SAMPLER_H
#define SAMPLER_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

#define MAX_PERCENT   80   /* max percent of syringe for sample, G4513 */

#define NORMAL        0    /* G4513 mode definitions */
#define TWO_LAYER     1
#define THREE_LAYER   2
#define TRANSFER_1    3

#define SAMP_POS     937   /* 71.7mm, 220 steps higher than ss */

#define WASTEB2      -10
#define WASTEA2       -9
#define WASHB3        -8
#define WASHA3        -7
#define WASTEC        -7   /* G2880 sample waste */
#define WASHA2        -6
#define WASHA         -5
#define WASTEA        -4
#define WASHB         -3
#define WASTEB        -2
#define WASHB2        -1

/* Solvent/Waste definitions for G4513 turret */
#define S_B           -1
#define S_C           -2
#define S_WASH_A1     -3
#define S_WASH_A2     -4
#define S_WASH_A3     -5
#define S_WASH_A4     -6
#define S_WASH_A5     -7
#define S_WASH_A6     -8
#define S_WASH_B1     -9
#define S_WASH_B2     -10
#define S_WASH_B3     -11
#define S_WASH_B4     -12
#define S_WASTE_A1    -13
#define S_WASTE_A2    -14
#define S_WASTE_A3    -15
#define S_WASTE_B1    -16
#define S_WASTE_B2    -17
#define S_NO_MOVE     -20

/* Syringe positioning for G4513 tower */
#define S_SYR_HOME    -1
#define S_SYR_NO_MOVE -2
#define S_SYR_SAMP    -6

#define SPLIT_INJPOS  220
#define ONCOL_INJPOS   34  /* 18.6 mm deeper */
#define CIS4_INJPOS   230  /* UNUSED ?? */

#define ALS_START_DELAY  256   /* 500 ms */

#define ALS_DOOR_MASK    0x01
#define ALS_MOUNTED_MASK 0x02
#define ALS_BUSY_MASK    0x08

#define SOLV_SAVE_VERSION 1006   /* minimum solvent saver version is 10.06 */


#define TOWER_MAX_PUMPS     15
#define TOWER_MAX_VISCOSITY  7
#define TOWER_MAX_WASHES    15
#define TOWER_MAX_DWELL    100
#define TOWER_MIN_SKIM     -20
#define TOWER_MAX_SKIM     300
#define TOWER_MAX_SYRINGE 5000
#define TOWER_MIN_SYRINGE    5

#define CSI_MAX_DRAW      10000
#define CSI_MIN_DRAW        100
#define MIN_DRAW_FACTOR       6    /* 6 * syr / 100 (syr in .1 ul) */
#define MAX_DRAW_FACTOR       6    /* 6  * syr (syr in .1ul) */
#define MIN_INJ_DISP_FACTOR   2    /* 2 * syr / 100 (syr in .1 ul) */
#define MIN_SOLV_DISP_FACTOR  6    /* 6 * syr / 100 (syr in .1 ul) */
#define MAX_DISP_FACTOR      60    /* 60  * syr (syr in .1ul) */
#define CSI_MAX_WASH_DISP 10000
#define CSI_MIN_WASH_DISP    10
#define CSI_MAX_WASH_VOL      7
#define CSI_MIN_WASH_VOL      2
#define CSI_MAX_INJ_DISP  30000
#define CSI_MIN_INJ_DISP     10  /* 0 used for OFF */
#define ALS_MIN_REPS          1
#define ALS_MAX_REPS        100
#define ALS_MIN_DELAY         0
#define ALS_MAX_DELAY       100
#define BCR_MIN_POSITION      1
#define BCR_MAX_POSITION     19

#define CONFIG_STR          14
#define CONFIG_WAIT          2   /* 1 to 2 seconds */

#define C_IDLE               0
#define C_ID                 1
#define C_CO                 2

#define TRAYBUSY        0x0002   /* busy flags (Tray_Busy) */
#define BCRBUSY         0x0001

#define MAX_BARCODE         32   /* Longest allowed barcode, truncates if longer */

#define FRONT_TRANSFER_POS  1045  /* new positions 10-15-97 */
#define FRONT_BOTTLE3_POS   1035  /* special case - avoid turret stop */
#define BACK_TRANSFER_POS   1260
#define F2913_TRANSFER_POS   991
#define B2913_TRANSFER_POS  1260
#define MAX_TUR                6

enum         {ZPOS, RPOS, TPOS};

typedef  INT16        BTLPOS[3];

enum    {FRONTINJLOC = 1, BACKINJLOC, TRAYLOC, NULLLOC};

typedef struct {INT16  typ;
                INT16 *btlnumptr;
                INT16  btlnum;
                INT16  tpos;
                BTLPOS pos; } BTLLOC_T, *pBTLLOC_T;

#define MINZPOS          0
#define MAXZPOS       1700
#define ZPOSDELTA       21     /* For bottle pickup adjust */
#define TPOSDELTA        4     /* For Tray arm theta adjust */
#define MINRPOS       1300
#define MAXRPOS       5500
#define MINTPOS        200
#define MAXTPOS       4050

#define FAST             0     /* same as "OFF" */
#define SLOW             1     /* same as "ON"  */
#define VARIABLE         2

enum {Enabled, VolumeStop, Pumps, Viscosity, SampleWash, PostWashA, PostWashB,
      SlowPlunger, PreDwell,  PostDwell,  SkimEnable, SkimDepth, PreWashA,
      PreWashB};

enum {COMM_ERR=1, SYRINGE_ERR, TURRET_ERR, PLUNGER_ERR, INJECT_ERR, CPU_ERR, FAULT_ERR, RESET_ERR, ALIGN_ERR};

enum {Err_NoError, Err_Zhome=100, Err_Rhome, Err_Thome, Err_BtlInClaw,
      Err_ClawEmpty, Err_StopKey, Err_NoTower, Err_NoTray, Err_NoBCR,
      Err_Front=109, Err_Back=119,
      Err_Ovld=129, Err_Stalled, Err_NoBarCode, Err_BCRchip,
      /*   Name Table Errors  */
      Err_NameIndex,  Err_NoName, Err_BadName, Err_BadLocation, Err_UnknownName,
      /*   Parsing/Parameter errors  */
      Err_InvParameter, Err_NoComma, Err_NoSemicolon, Err_NumError,
      Err_SetptRange, Err_SetptError, Err_RangeTray, Err_RangeTpos,
      Err_InvBottle, Err_BtlLost, Err_InvalidSet, Err_InetConfig,
      Err_SeqAborted, Err_NoSequence, Err_TimeOut, Err_HangUp, Err_Unknown,
      /*    Error log messages   */
      Err_LogFull=161, Err_EndOfLog, Err_LogLocked, Err_Tower,
      Err_Injector=166, Err_NotTrayInj,
      Err_SyrHome = 200, Err_Syringe, Err_SyrAlign, Err_TurNotClear, Err_SyrPosition,
      Err_SyrStalled,  Err_SyrTimeout,
      Err_TurHome = 220, Err_NewTurret, Err_TurPosition, Err_NoTurret,
      Err_Turret, Err_WrongTurret,
      Err_PlnHome1 = 240, Err_PlnHome2, Err_Plunger, Err_PlnPosition, Err_PlnNotHome,
      Err_PlnStuck, Err_BadPlunStop, Err_PlnHomeOn,
      Err_Inject = 260, Err_FrSolvent, Err_BkSolvent,
      Err_NotMounted = 280, Err_DoorOpen, Err_NotAligned, Err_NotReady, Err_Timeout,
      Err_Reset = 300,
      Err_DvcError = 400};


/*****************************/
/*   MACROS                  */
/*****************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef enum
{
   INJECT_IDLE       = 0,
   INJECT_MOVE       = 1,
   INJECT_MOVE2      = 2,
   INJECT_FBCR2      = 3,
   INJECT_FBCR3      = 4,
   INJECT_BBCR       = 5,
   INJECT_BBCR2      = 6,
   INJECT_BBCR3      = 7,
   INJECT_MOVE_END   = 8,
   INJECT_INIT       = 9,
   INJECT_PRE_WAIT   = 10,
   INJECT_PRE_WASH   = 11,
   INJECT_OVERLAP    = 12,
   INJECT_LOAD       = 13,
   INJECT_READY_WAIT = 14,
   INJECT_INJECT     = 15,
   INJECT_POSTWASH   = 16,
   INJECT_RETURN     = 17,
   INJECT_RETURN2    = 18,
   INJECT_RETURN_END = 19,
   INJECT_COMPLETE   = 20,
   INJECT_ERROR      = 21

} INJSTATE;

typedef enum
{
   T_IDLE       = 0,
   T_INIT       = 1,
   T_INIT_CHK   = 2,
   T_CHK_REPLY  = 3,
   T_INIT_WAIT  = 4,
   T_CLEAN      = 5,
   T_CLEAN2     = 6,
   T_PREWASH_A  = 7,
   T_PREWASH_B  = 8,
   T_SAMPWASH   = 9,
   T_SAMP_WAIT  = 10,
   T_PRE_DONE   = 11,
   T_LOAD_SYR   = 12,
   T_LOAD_WAIT  = 13,
   T_LOAD_DONE  = 14,
   T_INJECT     = 17,
   T_C_AIRGAP   = 18,
   T_B_LOAD     = 19,
   T_B_AIRGAP   = 20,
   T_A_LOAD     = 21,
   T_ABC_INJECT = 22,
   T_INJ_WAIT   = 23,
   T_INJ_RDY    = 24,
   T_POSTWASH_A = 25,
   T_POSTWASH_B = 26,
   T_DONE       = 27,
   T_DONE_REPLY = 28,
   T_FINISHED   = 29,
   T_ERROR      = 30

} TINJSTATE;

typedef enum
{
   MOVE_IDLE       = 0,
   MOVE_CHECK      = 1,
   MOVE_GET        = 2,
   MOVE_PUT        = 3,
   MOVE_COMPLETE   = 4,
   MOVE_ERROR      = 5

} MOVESTATE;

typedef enum
{
   GETPUT_IDLE       = 0,
   GETPUT_TURRET     = 1,
   GETPUT_TURRET2    = 2,
   GETPUT_GET        = 3,
   GETPUT_GET2       = 4,
   GETPUT_PUT        = 5,
   GETPUT_PUT2       = 6,
   GETPUT_CHECK      = 7,
   GETPUT_CHECK2     = 8

} GETPUTSTATE;

typedef enum
{
   RTNALL_IDLE       = 0,
   RTNALL_CHECK      = 1,
   RTNALL_CLAW       = 2,
   RTNALL_FINJ       = 3,
   RTNALL_FINJ2      = 4,
   RTNALL_FINJ3      = 5,
   RTNALL_FINJ4      = 6,
   RTNALL_BINJ       = 7,
   RTNALL_BINJ2      = 8,
   RTNALL_BINJ3      = 9,
   RTNALL_BINJ4      = 10,
   RTNALL_BCR        = 11,
   RTNALL_BCR2       = 12,
   RTNALL_BCR3       = 13,
   RTNALL_COMPLETE   = 14

} RTNALLSTATE;

typedef enum
{
   TRAYPOS_IDLE       = 0,
   TRAYPOS_START      = 1

} TRAYPOSSTATE;

typedef enum
{
   BCR_IDLE       = 0,
   BCR_FROM       = 1,
   BCR_READ       = 2,
   BCR_READ2      = 3,
   BCR_TO         = 4,
   BCR_COMPLETE   = 5

} RBCRSTATE;

typedef enum
{
   MIX_IDLE       = 0,
   MIX_START      = 1

} MIXSTATE;

typedef enum
{
   NO_TWR         = 0,   /* "NO_TOWER" aready used by TOWER_MODE */
   G2613          = 1,
   G2880          = 2,
   G2913          = 3,
   G4513          = 4

} TOWER_TYPE;

typedef enum
{
   NO_TRAY        = 0,
   G2614          = 1,
   G2914          = 2,
   G2916          = 3,
   G4514          = 4

} TRAY_TYPE;

typedef enum
{
   AB_MODE        = 0,   /* Wash A, B */
   AB2_MODE       = 1,   /* Wash A, A2, B, B2 */
   AB3_MODE       = 2    /* Wash A, A2, A3, B, B2, B3 */

} WASH_MODE;

typedef struct
{
   INT8  enabled;
   INT8  mode;
   INT8  volume_stop;
   INT8  volume_percent;
   INT8  b_vol;
   INT8  c_vol;
   INT8  air_gap;
   INT8  pumps;
   INT8  viscosity;
   INT8  sample_wash;
   INT8  post_wash_a;
   INT8  post_wash_b;
   INT8  slow_plunger;
   INT8  pre_dwell;
   INT8  post_dwell;
   INT8  skim_enable;
   INT8  pre_wash_a;
   INT8  pre_wash_b;
   INT16 skim_depth;
   INT16 inj_draw;
   INT16 inj_disp;
   INT16 solv_draw;
   INT16 solv_disp;

} ALS_SETPTS;

typedef struct
{
   INT16 draw;
   INT16 wash_disp;
/* INT8  wash_vol;     Vireo uses wash_volume, not reset at power on */
   INT16 inj_disp;

} CSI_SETPTS;

typedef struct
{
   POSITION   posn;

   BIT8       present;

   U_INT16    port;

   INT16      samp_depth;
   INT16      wash_depth;
   U_INT16    align_err;
   U_INT16    last_align_err;
   U_INT16    inj_err;
   U_INT16    max_bottle;
   U_INT16    max_stop;
   INT32      syringe_size;
   INT32      version;
   U_INT32    c_timer;
   INT8       c_state;
   BIT8       have_config;
   BIT8       config[CONFIG_STR];
   BIT8       uses_tray;
   TOWER_TYPE tower_type;

   BIT8       ping[3];
   BIT8       ping_sent;
   BIT8       clean;
   BIT8       send_fan;
   BIT8       send_time;
   BIT8       send_stop;
   U_INT8     align_state;
   U_INT8     last_align_state;

   INT8       sample;
   INT8       tray_sample;
   BIT8       inj_reply;
   TINJSTATE  inj_state;

   INT16      tur[MAX_TUR];

} TOWER_TBL, *pTOWER_TBL;

typedef struct
{
   ALS_SETPTS setpts;
   CSI_SETPTS csi;

   INT8       wash_volume;
   INT8       ss_onoff;
   WASH_MODE  extend_solv;
   INT8       last_washa;
   INT8       last_washb;
   INT8       waste_use;
   INT8       last_wastea;
   INT8       last_wasteb;
   INT8       last_sampwaste;
   INT8       fan_on_off;
   INT8       rep;
   INT8       delay;
   INT8       nl_onoff;
   INT8       syr_vol;

   BIT8       barcode[MAX_BARCODE+1];

   pTOWER_TBL twr;

} ALS_TBL, *pALS_TBL;

/*****************************/
/*   EXTERNALS               */
/*****************************/

#pragma SECTION DATA=pfail

extern ALS_TBL      TowerF;
extern ALS_TBL      TowerB;
extern TOWER_TBL    Tower1;
extern TOWER_TBL    Tower2;
extern BIT8         Swapped;
extern BIT8         Tray_Enabled;
extern BIT8         BCR_Enabled;
extern BIT8         Enable3of9;
extern BIT8         Enable2of5;
extern BIT8         EnableUPC;
extern BIT8         EnableChk;
extern INT8         BCRpos;
extern TRAY_TYPE    Tray_Type;

extern BIT8         Enable_Solv_Save;
extern U_INT8       Host_Control;

extern BIT8         Barcode[MAX_BARCODE+1];

extern INT16        BtlInClaw;
extern INT16        BCRbtlnum;

extern INT16        Z_Delta;
extern INT16        F_T_Delta;
extern INT16        B_T_Delta;

#pragma SECTION UNDO


extern U_INT32      Init_Timer;
extern U_INT32      Tray_C_Timer;
extern INT16        Als_Start_Timer;

extern U_INT16      Inject_Error;
extern U_INT16      Tray_Max_Bottle;
extern BIT8         Tray_Config[CONFIG_STR];
extern BIT8         Tray;
extern INT8         BCR_Type;
extern INT8         Tray_C_State;
extern BIT8         Tray_Have_Config;

extern BIT8         Inj_Status;
extern INJSTATE     Inj_Cmd_State;
extern BIT8         Inject_Request;
extern BIT8         No_Wait;
extern BIT8         Tray_Active;
extern BIT8         BCR_Active;
extern BIT8         Injector_Overlap;

extern BIT8         Tray_Busy;    /* includes BCR */
extern INT8         Tray_State;
extern INT8         BCR_State;
extern BIT8         Move_Request;
extern BIT8         RtnAll_Request;
extern BIT8         Bcr_Request;
extern BIT8         Tray_Reply;
extern BIT8         Get_Not_Put;
extern BIT8         Inject_Done;
extern BIT8         Move_Done;
extern BIT8         RtnAll_Done;
extern BIT8         Bcr_Done;
extern BIT8         Update_Bcr;
extern MOVESTATE    Move_State;
extern GETPUTSTATE  GetPut_State;
extern RTNALLSTATE  RtnAll_State;
extern TRAYPOSSTATE TrayPos_State;
extern RBCRSTATE    ReadBcr_State;
extern MIXSTATE     Mix_State;
extern BTLLOC_T     Move_From;
extern BTLLOC_T     Move_To;
extern pBTLLOC_T    GetPut_Loc;
extern pALS_TBL     GetPut_Als;
extern U_INT16      Tray_Error;
extern INT16        Tpos;         /* For RTN_ALL */

extern BIT8         Als_Power_Supply;

extern U_INT8       Inject_State;
extern BIT8         Watch_InjectorF;
extern BIT8         Watch_InjectorB;
extern BIT8         Watch_Tray;

extern BIT8         Headspace;
extern BIT8         Try_Headspace_Next;

extern const BTLPOS BtlTable[];
extern const BTLPOS InjPos6890[];
extern const BTLPOS InjPos6890_2913[];
extern const INT8   Relay_A_Tbl[];
extern const INT8   AB_Relay_A_Tbl[];
extern const INT8   AB_Relay_B_Tbl[];


/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

TOWER_TYPE GetTowerType (POSITION p);

void   SetAlsDefaults(void);
void   SetCsiDefaults(pALS_TBL als);
BIT8   IsSmallWonder (pTOWER_TBL twr);
void   CheckForTower (pTOWER_TBL twr);
void   CheckForTray (void);
void   CheckForHeadspace (void);
void   AintGotTower (pTOWER_TBL twr);
void   AintGotSampler (void);
void   PingTimeout (U_INT16 port);
void   EraseBarcodes (void);
BIT8   TrayBusy (void);
BIT8   BcrBusy (void);
BIT8   UsePercent (pALS_TBL als);

void SendTime(U_INT16 port);       /* These are for ALS TASK only! */
void HaltTower (pTOWER_TBL twr);
void HaltTray (void);
void SendFan(pTOWER_TBL twr);
void HandleTowerInject(pTOWER_TBL twr);
void HandleInjectCommand(void);
void HandleMoveCommand(void);
void HandleGetPut (void);
void HandleReturnAll (void);
void HandleTrayPos (void);
void HandleReadBcr (void);
void HandleMix(void);
void UpdateBcr(void);

#endif
